package com.xneure.function.node;

import java.util.ArrayList;
import java.util.List;
import com.xneure.code.exception.FunctionException;
import com.xneure.function.mode.Function;
import com.xneure.function.mode.FunctionBase;
import com.xneure.utils.ObjectUtil;

public class FunctionNode implements Node<Function>{
	private Function function;
	private List<Object> params = new ArrayList<>();
	private Token token;
	private RelationNode operator;

	public FunctionNode(FunctionBase fun){
		this.function = fun;
	}

	public void setFunction(Function function){
		this.function = function;
	}

	public Function getFunction(){
		return function;
	}

	public RelationNode getOperatorNode(){
		return operator;
	}

	public void setOperatorNode(RelationNode operator){
		this.operator = operator;
	}

	public void addParam(Object value){
		params.add(value);
	}

	public void verify(){
		try{
			if(ObjectUtil.isContainNull(params,function)){
				return;
			}
			int size = Math.max(params.size(),function.getParamCount());
			for(int i = 0;i < size;i++){
				Object value = null;
				if(i < params.size()){
					value = params.get(i);
				}
				if(value instanceof Node){
				}else{
					function.setParam(i,value);
				}
			}
		}catch(FunctionException e){
			ObjectUtil.modifyFieldValue(e,"detailMessage",String.format("公式(%1s:%2s) %3s %4s",token.line,token.endPoint,function.getName(),e.getMessage()));
			throw e;
		}
	}

	@Override
	public Token getToken(){
		return token;
	}

	public void setToken(Token token){
		this.token = token;
	}

	@Override
	public String toString(){
		return function.toString();
	}
}
